﻿using System;
using Framework.MVC;
using Identity.Model;
using Identity.IService.Entry;
using Log.Model;
using Billing.Model;
using Billing.IService.Entry;
using Recharge.Entry;


namespace Recharge.Models
{
    public class TBReChargeOwnerModel : AsyncModelBase, IWriteLog, IValidateIdentity, IValidateVerifyCode, INewRechargeOrder
    {

        #region Field

        private const string m_empty_verifycode = "tbcge0000";
        private const string m_error_verifycode = "tbcge0001";
        private const string m_complete_verifycode = "tbcge0002";
        private const string m_error_verifyvalue = "tbcge0003";
        private const string m_complete_verifyvalue = "tbcge0004";
        private const string m_empty_identity = "tbcge0005";
        private const string m_error_identity = "tbcge0006";
        private const string m_complete_identity = "tbcge0007";
        private const string m_error_neworder_amount = "tbcge0008";
        private const string m_error_neworder = "tbcge0009";
        private const string m_complete_neworder = "tbcge0010";


        /*private const string m_error_get_account = "answerg05";
        private const string m_complete_get_account = "answerg06";
        private const string m_error_get_answers = "answerg07";
        private const string m_complete_get_answers = "answerg08";
        private const string m_error_new_identity = "answerg09";
        private const string m_complete_new_identity = "answerg10";
        */
        private const string m_resource_verify = "vc00";
        private const string m_resource_user = "u000";

        private const string m_entry = "entry";
        
        #endregion Field

        #region AsyncModelBase

        protected override void OnInit(Type type)
        {
            OnBeginRequest();
            this.ValidateVerifyCode(m_resource_verify, m_empty_verifycode, m_error_verifycode, m_complete_verifycode, m_error_verifyvalue, m_complete_verifyvalue);

        }

        #endregion AsyncModelBase

        #region Write Log

        public void OnWriteLogComplete(string eventCode)
        {
            OnEndRequest();
        }

        public void OnWriteLogError(string eventCode)
        {
            OnEndRequest();
        }

        #endregion Write Log

        #region IValidateVerifyCode

        public string VerifyCode { get; set; }

        public string Source { get; set; }

        public string VerifyValue { get; set; }

        public void OnValidateVerifyCodeComplete(string key, IdentityEntry entry)
        {
            OnBeginRequest();
            this.Log(key, entry);
            if (string.Equals(key, m_complete_verifyvalue))
            {
                this.ValidateIdentity(m_resource_user, m_empty_identity, m_error_identity, m_complete_identity);
            }

        }

        public void OnValidateVerifyCodeError(string key)
        {
            this.Log(key, new IdentityEntry { code = VerifyCode, source = Source, resource = m_resource_verify });
        }

        #endregion IValidateVerifyCode

        #region IValidateIdentity

        public string IdentityCode { get; set; }

        public void OnValidateIdentityComplete(string key, IdentityEntry entry)
        {
            OnBeginRequest();
            this.Log(key, entry);
            
            RechargeOrderOwner = entry.value;
            RechargeOrderReceiver = entry.value;
            this.NewRechargeOrder(m_error_neworder_amount,m_error_neworder, m_complete_neworder);
        }

        public void OnValidateIdentityError(string key)
        {
            this.Log(key, new IdentityEntry { code = IdentityCode, source = Source, resource = m_resource_user });
        }

	    #endregion IValidateIdentity

        #region INewRechargeOrder

        public int RechargeOrderAmount { get; set; }

        public string RechargeOrderOwner { get; set; }

        public string RechargeOrderReceiver { get; set; }

        public string RechargeOrderType { get; set; }

        public string RechargeOrderSource { get; set; }

        public void OnNewRechargeOrderComplete(string key, RechargeOrderEntry entry)
        {
            OnComplete(key,entry);
            this.Log(key, entry);
        }

        public void OnNewRechargeOrderError(string key)
        {
            this.Log(key, new RechargeOrderEntry { owner = RechargeOrderOwner, receiver = RechargeOrderReceiver, amount = RechargeOrderAmount, type = RechargeOrderType });
        }

        #endregion INewRechargeOrder



    }
}